#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog, QSystemTrayIcon, QAction, QMenu
from PyQt5.QtCore import QThread, pyqtSignal

from UI_MyFTPServerConfig import Ui_Form
from MyFTPServer import MyFTPServer


class FTPServer_GUI(QWidget, Ui_Form):

    def __init__(self):
        super(FTPServer_GUI, self).__init__()
        self.setupUi(self)
        self.initUI()

    def initUI(self):
        
        self.startserverAction = QAction("启动FTP", self)
        self.stopserverAction = QAction("停止FTP",self)
        self.configAction = QAction("配置", self)
        self.logAction = QAction("日志", self)
        self.aboutAction = QAction("关于", self)
        self.quitAction = QAction("退出", self)
        
        self.trayIconMenu = QMenu(self)
        self.trayIconMenu.addAction(self.startserverAction)
        self.trayIconMenu.addAction(self.stopserverAction)
        self.trayIconMenu.addAction(self.configAction)
        self.trayIconMenu.addSeparator()
        self.trayIconMenu.addAction(self.aboutAction)
        self.trayIconMenu.addAction(self.logAction)
        self.trayIconMenu.addSeparator()
        self.trayIconMenu.addAction(self.quitAction)

        self.trayIcon = QSystemTrayIcon(self)
        self.trayIcon.setContextMenu(self.trayIconMenu)
        icon = QIcon()
        icon.addPixmap(QPixmap("FTP.png"), QIcon.Normal, QIcon.Off)
        self.trayIcon.setIcon(icon)
        self.trayIcon.show()
        
        self.pushButton_path.clicked.connect(self.button_path_clicked)
        self.pushButton_cancel.clicked.connect(self.quitApp)
        self.pushButton_ok.clicked.connect(self.startFTPServer)
        self.quitAction.triggered.connect(self.quitApp)
        self.startserverAction.triggered.connect(self.startFTPServer)
        self.stopserverAction.triggered.connect(self.stopFTPServer)


    def button_path_clicked(self):
        directory_path = QFileDialog.getExistingDirectory(None, "选取文件夹", "~/")
        self.lineEdit_path.setText(directory_path)
        
    def quitApp(self):
        app = QApplication.instance()
        app.quit()

    def startFTPServer(self):
        _username = self.lineEdit_username.text()
        _password = self.lineEdit_password.text()
        _port = self.lineEdit_port.text()
        _directory = self.lineEdit_path.text()
        self.mythread = MyFTPThread(_username, _password, _port, _directory)
        self.mythread.start()

    def stopFTPServer(self):
        self.mythread.finished()

class MyFTPThread(QThread):
    signal = pyqtSignal(str)
    def __init__(self, username, password, port, directory):
        super(MyFTPThread, self).__init__()
        self.username = username
        self.password = password
        self.port = port
        self.directory = directory

    def run(self):
        MyFTPServer(self.username, self.password, self.port, self.directory)
        

if __name__ == '__main__':
    app = QApplication(sys.argv)
    QApplication.setQuitOnLastWindowClosed(False)
    ftp_server = FTPServer_GUI()
    ftp_server.show()
    
    sys.exit(app.exec_())
